﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using AvatarHelper;
using Avatar.Model;
using AvatarMembership.Model;
using System.Web.Security;

public partial class Management_PersonInfo : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            this.gvBind(0);
        }
    }
    protected void gvBind(int _page)
    {
        using (var _adc = DBHelper.GetMembershipDataContext())
        {
            var _role = _adc.aspnet_Roles.FirstOrDefault(r => r.RoleName == "个人用户");
            if (_role != null)
            {
                lbl_Current.Text = _page.ToString();
                lbl_Total.Text = (_adc.aspnet_UsersInRoles.Count(u => u.RoleId == _role.RoleId) / 15).ToString();
                ListView1.DataSource = _adc.aspnet_UsersInRoles.Join(_adc.aspnet_Users, ur => ur.UserId, u => u.UserId, (ur, u) => new { Userid = u.UserId, UserName = u.UserName, LastLogin = u.LastActivityDate, Roleid = ur.RoleId }).Where(u => u.Roleid == _role.RoleId).OrderByDescending(u => u.LastLogin).Skip(15 * _page).Take(15);
                ListView1.DataBind();
            }
        }
    }
    protected void btn_Up_Click(object sender, EventArgs e)
    {
        var _page = Convert.ToInt32(lbl_Current.Text);
        if (_page != 0) _page -= 1;
        this.gvBind(_page);
    }
    protected void btn_Down_Click(object sender, EventArgs e)
    {
        var _page = Convert.ToInt32(lbl_Current.Text);
        if (_page != Convert.ToInt32(lbl_Total.Text)) _page += 1;
        this.gvBind(_page);
    }
    protected void btn_Goto_Click(object sender, EventArgs e)
    {
        int _page = 1;
        try
        {
            if (!string.IsNullOrEmpty(txt_PageNumber.Text)) _page = Convert.ToInt32(txt_PageNumber.Text);
        }
        catch { }
        this.gvBind(_page);
    }
    protected PersonalBasicInfo getPersonBasicInfo(string _user)
    {
        using (var _adc = DBHelper.GetDataContext())
        {
            var _resume = _adc.Resume.FirstOrDefault(r => r.User == _user);
            if (_resume == null) return null;
            return _resume.PersonalBasicInfo;
        }
    }
    protected void btn_Search_Click(object sender, EventArgs e)
    {
        try
        {
            int _page = 0;
            var _keyWord = txt_KeyWord.Text != null ? txt_KeyWord.Text : null;
            if (string.IsNullOrEmpty(_keyWord)) throw new Exception("关键字不能为空");
            using (var _adc = DBHelper.GetMembershipDataContext())
            {
                var _role = _adc.aspnet_Roles.FirstOrDefault(r => r.RoleName == "个人用户");
                if (_role != null)
                {
                    lbl_Current.Text = _page.ToString();
                    lbl_Total.Text = (_adc.aspnet_UsersInRoles.Count(u => u.RoleId == _role.RoleId) / 15).ToString();
                    ListView1.DataSource = _adc.aspnet_UsersInRoles.Join(_adc.aspnet_Users, ur => ur.UserId, u => u.UserId, (ur, u) => new { Userid = u.UserId, UserName = u.UserName, LastLogin = u.LastActivityDate, Roleid = ur.RoleId }).Where(u => u.Roleid == _role.RoleId && u.UserName.Contains(_keyWord)).OrderByDescending(u=>u.LastLogin).Skip(15 * _page).Take(15);
                    ListView1.DataBind();
                }
            }
        }
        catch (Exception _e)
        {
            lbl_Error.Text = _e.Message;
        }
    }
    protected void btn_DelUser_Click(object sender, EventArgs e)
    {
        try
        {
        using (var _adc = DBHelper.GetDataContext())
        {
            Button _btn = sender as Button;
            var _user = _btn.CommandArgument;

            var _resume = _adc.Resume.FirstOrDefault(r => r.User == _user);
            if (_resume != null)
            {
                if (_resume.LanguageLevel != null)
                {
                    _adc.LanguageLevel.DeleteOnSubmit(_resume.LanguageLevel);
                    _adc.SubmitChanges();
                }

                if (_resume.WorkExperience.Count > 0)
                {
                    _adc.WorkExperience.DeleteAllOnSubmit(_resume.WorkExperience);
                    _adc.SubmitChanges();
                }

                if (_resume.TrainingExperience.Count > 0)
                {
                    _adc.TrainingExperience.DeleteAllOnSubmit(_resume.TrainingExperience);
                    _adc.SubmitChanges();
                }

                if (_resume.Education.Count > 0)
                {
                    _adc.Education.DeleteAllOnSubmit(_resume.Education);
                    _adc.SubmitChanges();
                }

                if (_resume.InterViewRecord.Count > 0)
                {
                    _adc.InterViewRecord.DeleteAllOnSubmit(_resume.InterViewRecord);
                    _adc.SubmitChanges();
                }

                if (_resume.JobApplicationRecord.Count > 0)
                {
                    _adc.JobApplicationRecord.DeleteAllOnSubmit(_resume.JobApplicationRecord);
                    _adc.SubmitChanges();
                }

                if (_resume.LanguageAbility.Count > 0)
                {
                    _adc.LanguageAbility.DeleteAllOnSubmit(_resume.LanguageAbility);
                    _adc.SubmitChanges();
                }

                if (_resume.ResumeFavorites.Count > 0)
                {
                    _adc.ResumeFavorites.DeleteAllOnSubmit(_resume.ResumeFavorites);
                    _adc.SubmitChanges();
                }

                if (_resume.SelfEvaluation != null)
                {
                    _adc.SelfEvaluation.DeleteOnSubmit(_resume.SelfEvaluation);
                    _adc.SubmitChanges();
                }

                var _pInfo = _adc.PersonalBasicInfo.FirstOrDefault(p => p.ID == _resume.BasicInformationID);
                var _jobIntention = _adc.JobIntention.FirstOrDefault(i => i.ID == _resume.JobIntentionID);

                _adc.Resume.DeleteOnSubmit(_resume);
                _adc.SubmitChanges();

                try
                {
                    if (_pInfo != null)
                    {
                        _adc.PersonalBasicInfo.DeleteOnSubmit(_pInfo);
                        _adc.SubmitChanges();
                    }

                    if (_jobIntention != null)
                    {
                        _adc.JobIntention.DeleteOnSubmit(_jobIntention);
                        _adc.SubmitChanges();
                    }
                }
                catch
                { }
            }
            Membership.DeleteUser(_user);

            Response.Redirect(Request.Url.ToString());
        }
        }
        catch (Exception _e)
        {
            lbl_Error.Text = _e.Message;
        }
    }
}
